package com

import java.util.*
import java.util.concurrent.atomic.AtomicInteger

object VerificationUtils {
    private val strGen = Random()
    private val numGen = Random()
    private val numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        .toCharArray()
    private val numbers = "0123456789".toCharArray()

    /**  产生随机字符串   */
    @JvmStatic
    fun randomString(length: Int): String? {
        if (length < 1) {
            return null
        }
        val randBuffer = CharArray(length)
        for (i in randBuffer.indices) {
            randBuffer[i] = numbersAndLetters[strGen.nextInt(61)]
        }
        return String(randBuffer)
    }

    /**  产生随机数值字符串   */
    fun randomNumStr(length: Int): String? {
        if (length < 1) {
            return null
        }
        val randBuffer = CharArray(length)
        for (i in randBuffer.indices) {
            randBuffer[i] = numbers[numGen.nextInt(9)]
        }
        return String(randBuffer)
    }


    private const val maxvaluefive = 99999999
    private const val minvaluefive = 0
    private val atomic = AtomicInteger(minvaluefive)

    /** 生成序列号  */
    fun getSeqFive(coverPad: Int): String {
        while (true) {
            val current = atomic.get()
            val newValue = if (current >= maxvaluefive) minvaluefive else current + 1
            if (atomic.compareAndSet(current, newValue)) {
                return current.toString().padStart(coverPad, '0')
            }
        }
    }
}
